home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Appearance SDK 1.0.4 / Appearance Sample Code / Source / ColorPenState.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-16  |  3.5 KB  |  149 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ColorPenState.c
  3.  
  4.     Contains:    Utility routines to save and restore the graphics state of a grafport.
  5.  
  6.     Version:    Appearance 1.0 SDK
  7.  
  8.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                Edward Voas
  13.  
  14.         Other Contact:        7 of 9, Borg Collective
  15.  
  16.         Technology:            OS Technologies Group
  17.  
  18.     Writers:
  19.  
  20.         (edv)    Ed Voas
  21.  
  22.     Change History (most recent first):
  23.  
  24.          <1>     9/11/97    edv        First checked in.
  25. */
  26.  
  27. #include "ColorPenState.h"
  28. #include "ColorUtils.h"
  29. #include "Assertions.h"
  30.  
  31. //-----------------------------------------------------------------------------------------
  32. //    • GetColorAndPenState
  33. //
  34. //    Gets the current drawing environment and stores the data in state. We copy pen and back
  35. //    pix pats only if they are not type 0 (plain ol expanded black and white patterns).
  36. //-----------------------------------------------------------------------------------------
  37.  
  38. void
  39. GetColorAndPenState( ColorPenState* state )
  40. {
  41.     GrafPtr            curPort;
  42.     
  43.     GetPort( &curPort );
  44.     
  45.     state->pnPixPat = nil;
  46.     state->bkPixPat = nil;
  47.  
  48.     state->colorPort = IsColorGrafPort( curPort );
  49.  
  50.     state->bkPat = curPort->bkPat;
  51.     state->bkColor = curPort->bkColor;
  52.     state->fgColor = curPort->fgColor;
  53.  
  54.     if ( state->colorPort )
  55.     {
  56.         GetForeColor( &state->foreColor );
  57.         GetBackColor( &state->backColor );
  58.         
  59.             // If the pen pattern is not an old style pattern,
  60.             // copy the handle. If it is an old style pattern,
  61.             // GetPenState below will save the right thing.
  62.             
  63.         if ( (**((CGrafPtr)curPort)->pnPixPat).patType != 0 )
  64.         {
  65.             state->pnPixPat = ((CGrafPtr)curPort)->pnPixPat;
  66.         }
  67.         
  68.             // If the pen pattern is not an old style pattern,
  69.             // copy the handle, else get the old pattern into
  70.             // bkPat for restoring that way.
  71.             
  72.         if ( (**((CGrafPtr)curPort)->bkPixPat).patType != 0 )
  73.         {
  74.             state->bkPixPat = ((CGrafPtr)curPort)->bkPixPat;
  75.         }
  76.         else
  77.             state->bkPat = *(PatPtr)(*(**((CGrafPtr)curPort)->bkPixPat).patData);
  78.     }
  79.         
  80.     GetPenState( &state->pen );
  81.     state->textMode = curPort->txMode;
  82. }
  83.  
  84. //-----------------------------------------------------------------------------------------
  85. //    • SetColorAndPenState
  86. //
  87. //    Sets the current drawing environment based on the data in state.
  88. //-----------------------------------------------------------------------------------------
  89.  
  90. void
  91. SetColorAndPenState( ColorPenState* state )
  92. {
  93.     GrafPtr        curPort;
  94.     
  95.     GetPort( &curPort );
  96.  
  97.     SetPenState( &state->pen );
  98.  
  99.     if ( IsColorGrafPort( curPort ) && state->colorPort )
  100.     {
  101.         RGBForeColor( &state->foreColor );
  102.         RGBBackColor( &state->backColor );
  103.  
  104.         if ( state->pnPixPat )
  105.             PenPixPat( state->pnPixPat );
  106.  
  107.         if ( state->bkPixPat )
  108.             BackPixPat( state->bkPixPat );
  109.         else
  110.             BackPat( &state->bkPat );
  111.     }
  112.     else
  113.     {
  114.         BackPat( &state->bkPat );
  115.         ForeColor( state->fgColor );
  116.         BackColor( state->bkColor );
  117.     }
  118.  
  119.     TextMode( state->textMode );
  120. }
  121.  
  122. //-----------------------------------------------------------------------------------------
  123. //    • NormalizeColorAndPen
  124. //
  125. //    Sets up our environment to standard drawing fare.
  126. //-----------------------------------------------------------------------------------------
  127.  
  128. void
  129. NormalizeColorAndPen()
  130. {
  131.     RGBColor        black, white;
  132.     Pattern            whitePat = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  133.     GrafPtr            curPort;
  134.     
  135.     GetPort( &curPort );
  136.     
  137.     black.red = black.green = black.blue = 0x0000;
  138.     white.red = white.green = white.blue = 0xFFFF;
  139.     
  140.     if ( IsColorGrafPort( curPort ) )
  141.     {
  142.         RGBForeColor( &black );
  143.         RGBBackColor( &white );
  144.     }
  145.     PenNormal();
  146.     BackPat( &whitePat );
  147.     TextMode( srcOr );
  148. }
  149.